home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj9205.zip / 1005087A < prev    next >
Text File  |  1992-06-02  |  1KB  |  75 lines

  1.  
  2. Listing 2
  3.  
  4. //
  5. // rational.cpp
  6. //
  7. #include "mylib.h"
  8. #include "rational.h"
  9.  
  10. rational rational::operator-()
  11.     {
  12.     rational result(*this);
  13.     result.num = -result.num;
  14.     return result;
  15.     }
  16.  
  17. rational rational::operator++(int)
  18.     {
  19.     rational result(*this);
  20.     *this += 1;
  21.     return result;
  22.     }
  23.  
  24. rational rational::operator--(int)
  25.     {
  26.     rational result(*this);
  27.     *this -= 1;
  28.     return result;
  29.     }
  30.  
  31. rational &rational::operator+=(rational r)
  32.     {
  33.     num = num * r.denom + r.num * denom;
  34.     denom *= r.denom;
  35.     simplify();
  36.     return *this;
  37.     }
  38.  
  39. rational &rational::operator-=(rational r)
  40.     {
  41.     num = num * r.denom - r.num * denom;
  42.     denom *= r.denom;
  43.     simplify();
  44.     return *this;
  45.     }
  46.  
  47. rational &rational::operator*=(rational r)
  48.     {
  49.     num *= r.num;
  50.     denom *= r.denom;
  51.     simplify();
  52.     return *this;
  53.     }
  54.  
  55. rational &rational::operator/=(rational r)
  56.     {
  57.     num *= r.denom;
  58.     denom *= r.num;
  59.     simplify();
  60.     return *this;
  61.     }
  62.  
  63. void rational::put(FILE *f)
  64.     {
  65.     fprintf(f, "(%ld/%ld)", num, denom);
  66.     }
  67.  
  68. void rational::simplify()
  69.     {
  70.     long x = gcd(num, denom);
  71.     num /= x;
  72.     denom /= x;
  73.     }
  74.  
  75.